package com.bnz.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.bnz.common.PageUtils;
import com.bnz.common.Query;
import com.bnz.shop.dao.BrandDao;
import com.bnz.shop.dao.GoodsDescDao;
import com.bnz.shop.dao.SellerDao;
import com.bnz.shop.entity.*;
import com.bnz.shop.entity.group.Goods;
import com.bnz.shop.service.GoodsDescService;
import com.bnz.shop.service.ItemCatService;
import com.bnz.shop.service.ItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.bnz.shop.dao.GoodsDao;
import com.bnz.shop.service.GoodsService;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsService")
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
	@Autowired
	private GoodsDescService goodsDescService;
	@Autowired
	private SellerDao sellerDao;
	@Autowired
	private ItemCatService itemCatService;
	@Autowired
	private BrandDao brandDao;
	@Autowired
	private ItemService itemService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );
        return new PageUtils(page);
    }

    //1. 保存商品
	@Override
	@Transactional
	public void insert(Goods goods) {
		//1.1 添加tb_goods 表的数据
		this.save(goods.getGoods());
		//1.2 添加tb_goodsDesc 表的数据
		goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());
		goodsDescService.save(goods.getGoodsDesc());
		//1.3 为sku商品表添加数据
		addItem(goods);
	}

	//2. 添加sku商品列表
	private void addItem(Goods goods) {
		for (ItemEntity item : goods.getItemList()) {
			item.setTitle(goods.getGoods().getGoodsName());
			BrandEntity brand = brandDao.selectById(goods.getGoods().getBrandId());
			item.setBrand(brand.getName());
			ItemCatEntity itemCat = itemCatService.getById(goods.getGoods().getCategory3Id());
			item.setCategory(itemCat.getName());
			// SellerEntity seller = sellerDao.selectById(goods.getGoods().getSellerId());
			// item.setSeller(seller.getNickName());
			item.setSellerId(goods.getGoods().getSellerId());
			item.setUpdateTime(new Date());
			item.setCreateTime(new Date());
			item.setGoodsId(goods.getGoods().getId());
			item.setCategoryid(goods.getGoods().getCategory3Id());
			String itemImages = goods.getGoodsDesc().getItemImages();
			if(StringUtils.isNotBlank(itemImages)){
				List<Map> maps = JSON.parseArray(itemImages, Map.class);
				item.setImage(maps.get(0).get("url") + "");
			}
			itemService.save(item);

		}
	}

	//3. 根据商品id查询组合对象
	@Override
	public Goods findById(String id) {
    	//3.1 查询goods对象
		GoodsEntity goodsEntity = this.getById(id);
		//3.2 查询goodsDesc对象
		GoodsDescEntity goodsDescEntity = goodsDescService.getById(id);
		//3.3 查询item这个sku商品列表
		List<ItemEntity> itemList = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id));
		//3.4 返回查询到的对象
		return new Goods(goodsEntity,goodsDescEntity,itemList);
	}

	//4. 修改商品
	@Override
	@Transactional
	public void updateGoods(Goods goods) {
    	//4.1 修改tb_goods表
		this.updateById(goods.getGoods());
		//4.2 修改tb_goodsDesc表
		goodsDescService.updateById(goods.getGoodsDesc());
		//4.3 根据tb_goods中的goods_id外键在tb_item表中删除数据
		itemService.remove(new QueryWrapper<ItemEntity>().eq("goods_id",goods.getGoods().getId()));
		//4.4 再重新添加sku商品(tb_item表)
		addItem(goods);
	}
}